/*
 * Class: Login
 * 
 * Created on Sep 28, 2013
 * 
 * (c) Copyright by Minh Tran
 * Global Cybersoft VN.
 * Floor 3-4-5, Helios Building, Quang Trung Software City
 */
package com.minh.tran.sg.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.minh.tran.sg.Configuration;

/**
 * The Class Login.
 */
public class Login
{

    /** The Constant USER. */
    private static final String USER = "minhtran";

    /** The Constant PASS. */
    private static final String PASS = "minhtran";

    /** The shell. */
    protected Shell shell;

    /** The txt user. */
    private Text txtUser;

    /** The txt password. */
    private Text txtPassword;

    /** The config. */
    private final Configuration config;

    public Login(final Configuration config)
    {
        this.config = config;
    }

    /**
     * Launch the application.
     *
     * @param args the arguments
     */
    /*public static void main(String[] args)
    {
        try
        {
            Login window = new Login();
            window.open();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }*/

    /**
     * Open the window.
     */
    public void open()
    {
        Display display = Display.getDefault();
        this.createContents();
        this.shell.open();
        this.shell.layout();
        while (!this.shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents()
    {
        this.shell = new Shell();
        this.shell.setSize(323, 136);
        this.shell.setText("SWT Application");
        this.shell.setLayout(new FillLayout(SWT.HORIZONTAL));

        Composite composite = new Composite(this.shell, SWT.NONE);
        composite.setBounds(0, 0, 64, 64);
        composite.setLayout(new GridLayout(1, false));

        this.txtUser = new Text(composite, SWT.BORDER);
        this.txtUser.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.ITALIC));
        this.txtUser.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        this.txtPassword = new Text(composite, SWT.BORDER | SWT.PASSWORD);
        this.txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        Composite composite_1 = new Composite(composite, SWT.NONE);
        GridData gd_composite_1 = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
        gd_composite_1.heightHint = 31;
        composite_1.setLayoutData(gd_composite_1);

        Button btnLogin = new Button(composite_1, SWT.NONE);
        btnLogin.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                Login.this.login();
            }
        });
        btnLogin.setBounds(0, 0, 75, 25);
        btnLogin.setText("Login");

    }

    /**
     * Login.
     */
    protected void login()
    {
        String user = this.txtUser.getText();
        String pass = this.txtPassword.getText();
        System.out.println(user + "|" + pass);
        if (USER.equals(user) && PASS.equals(pass))
        {
            ConfigView window = new ConfigView(this.config);
            window.open();
        }
        this.shell.close();
    }

}
